#!/usr/bin/env python
# -*- coding: utf-8 -*-

__VERSION__ = '1.0.1'
__author__ = 'rx.wen218@gmail.com'

from Tkinter import *
from core.blogproviderfactory import BlogProviderType, BlogProviderFactory
from threading import Thread
from datetime import datetime, timedelta
import pdb

class BlogThread(Thread):
    def __init__(self, blog_provider, lst, start_date, end_date):
        Thread.__init__(self)
        self.start_date = datetime.strptime(start_date, Strings.DATE_FORMAT)
        self.end_date = datetime.strptime(end_date, Strings.DATE_FORMAT).replace(hour=23,minute=59,second=59)
        self.blog_provider = blog_provider
        self.lst = lst

    def run(self):
        posts = self.blog_provider.GetPosts(start_date = self.start_date, end_date = self.end_date)
        for post in posts:
            self.lst.insert(END, post.title)

class Strings:
    """
    Strings used in the app
    """
    DATE_FORMAT = "%Y-%m-%d"
    WENTERINFO_BLOGINFO_SRC = "源"
    WENTERINFO_BLOGINFO_DST = "目标"
    WENTERINFO_BLOGINFO_BLOG_TYPE = "Blog"
    WENTERINFO_BLOGINFO_DATE_TITLE = "请选择以下时间段内的博客"
    WENTERINFO_BLOGINFO_DATE_FROM = "从"
    WENTERINFO_BLOGINFO_DATE_TO = "到"
    WENTERINFO_BLOGINFO_CRED_TITLE = "登录信息"
    WENTERINFO_BLOGINFO_CRED_USRN = "用户名"
    WENTERINFO_BLOGINFO_CRED_PASD = "密码"


class BlogSelector(OptionMenu):
    """
    dropdownlist for selecting blog type
    """
    def __init__(self, frame, option_menu_event_handler = None, **kwargs):
        self.var = StringVar()
        self.var.set("Blogger") # default value
        # OptionMenu is old style class, so call its contructor directly
        OptionMenu.__init__(self, frame, self.var, 
                "Blogger","Wordpress","CSDN", "Cnblogs.com", 
                command=option_menu_event_handler, **kwargs)

    def curselection(self):
        return self.var.get()

class BlogInfoType:
    SOURCE = 1
    DEST = 2

class BlogInfo(Frame):
    """
    area for entering blog information
    """
    def __init__(self, root, blog_type=BlogInfoType.SOURCE):
        default_bg_color = "#F0F0F0" #"white"
        Frame.__init__(self, root, background=default_bg_color)
        
        self.title = Label(self, width=22, background=default_bg_color)
        self.title.grid(column=0, row=0, columnspan=2, pady=10, ipadx=20)
        if BlogInfoType.SOURCE == blog_type:
            self.title.config(text=Strings.WENTERINFO_BLOGINFO_SRC)
        else:
            self.title.config(text=Strings.WENTERINFO_BLOGINFO_DST)
        Label(self, text=Strings.WENTERINFO_BLOGINFO_BLOG_TYPE, background=default_bg_color).grid(column=0, row=1, stick=W)
        self.blog_type_list = BlogSelector(self)
        self.blog_type_list.grid(column=1, row=1, stick = S + N + E + W)
        Label(self, text="------------", background=default_bg_color).grid(column=0, row=2, columnspan=2)
        if BlogInfoType.SOURCE == blog_type:
            self.label_date = Label(self, text=Strings.WENTERINFO_BLOGINFO_DATE_TITLE, background=default_bg_color)
            self.label_date.grid(column=0, row=3, columnspan=2, stick=W)
            self.label_start_date = Label(self, text=Strings.WENTERINFO_BLOGINFO_DATE_FROM, background=default_bg_color)
            self.label_start_date.grid(column=0, row=4, stick=W)
            self.label_end_date = Label(self, text=Strings.WENTERINFO_BLOGINFO_DATE_TO, background=default_bg_color)
            self.label_end_date.grid(column=0, row=5, stick=W)
            self.start_date = Entry(self, background=default_bg_color)
            self.start_date.grid(column=1, row=4)
            self.start_date.insert(0, (datetime.now()-timedelta(7)).strftime(Strings.DATE_FORMAT))
            self.end_date = Entry(self, background=default_bg_color)
            self.end_date.grid(column=1, row=5)
            self.end_date.insert(0, datetime.now().strftime(Strings.DATE_FORMAT))
            Label(self, text="------------", background=default_bg_color).grid(column=0, row=6, columnspan=2)
        Label(self, text=Strings.WENTERINFO_BLOGINFO_CRED_TITLE, 
                background=default_bg_color).grid(column=0, row=7, columnspan=2, stick=W)
        Label(self, text=Strings.WENTERINFO_BLOGINFO_CRED_USRN, background=default_bg_color).grid(column=0, row=8, stick=W)
        Label(self, text=Strings.WENTERINFO_BLOGINFO_CRED_PASD, background=default_bg_color).grid(column=0, row=9, stick=W)
        self.user_name = Entry(self, background=default_bg_color)
        self.user_name.grid(column=1, row=8)
        self.user_name.bind("<FocusOut>", self.EventHandlerForAutoLogin)
        self.password = Entry(self, background=default_bg_color)
        self.password.grid(column=1, row=9)
        self.password.bind("<FocusOut>", self.EventHandlerForAutoLogin)

    def EventHandlerForAutoLogin(self, event):
        Login()

    def Login(self):
        #pdb.set_trace()
        print self.user_name.get()
        print self.password.get()
        print self.blog_type_list.curselection()

class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        img = PhotoImage(file="pics/main_background.gif")
        self.bg = Label(frame, image=img)
        self.bg.image =img 
        self.bg.grid(column=0,row=0,columnspan=3,rowspan=1)

        img2 = PhotoImage(file="logo.gif")
        self.hi = Button(frame, image=img2, bd=0, command=self.NextStep)
        self.hi.image = img2
        self.hi.grid(column=2,row=0)

        self.src_binfo = BlogInfo(frame)
        self.src_binfo.grid(row=0, column=0, padx=20)
        self.dst_binfo = BlogInfo(frame, BlogInfoType.DEST)
        self.dst_binfo.grid(row=0, column=1, pady=50)
        self.factory = BlogProviderFactory()

    def NextStep(self):
        self.src_provider = self.factory.GetBlogProvider(BlogProviderType.CNBLOGS, \
                "rxwen", "Wrx1415926")
        #posts = self.src_provider.GetPosts()
        other = Toplevel()
        otherlabel = Label(other, text = 'This is the other window', relief = RIDGE)
        otherlabel.grid()

        lst = Listbox(other)
#        lst.insert(END, "one")
#        lst.insert(END, "two")
#        lst.insert(END, "three")
        lst.grid(row=1)
        th = BlogThread(self.src_provider, lst, self.src_binfo.start_date.get(), self.src_binfo.end_date.get())
        th.start()

        print lst.curselection()
    
    def om_event(self, event):
        print "om_event " + event 


root = Tk()
#root.wm_iconbitmap("@logo.ico")
#root.iconbitmap("@logo.bmp")
root.wm_title("blog-porter")
app = App(root)
root.mainloop()
